<template>
  <div class="app-container">
    <el-card class="filter-container">
      <el-tabs v-model="activeTab" @tab-click="handleTabChange">
        <el-tab-pane label="待处理纠纷" name="pending"></el-tab-pane>
        <el-tab-pane label="所有纠纷" name="all"></el-tab-pane>
      </el-tabs>
      
      <div v-show="activeTab === 'all'">
        <el-form :inline="true" :model="listQuery" class="form-inline">
          <el-form-item label="纠纷状态">
            <el-select v-model="listQuery.status" placeholder="全部状态" clearable style="width: 150px">
              <el-option label="待处理" value="pending" />
              <el-option label="处理中" value="processing" />
              <el-option label="已解决" value="resolved" />
              <el-option label="已拒绝" value="rejected" />
            </el-select>
          </el-form-item>
          <el-form-item label="关键词">
            <el-input
              v-model="listQuery.keyword"
              placeholder="订单号/客户名称"
              style="width: 200px"
              class="filter-item"
              @keyup.enter="handleFilter"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleFilter">搜索</el-button>
            <el-button @click="resetFilter">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>

    <el-card>
      <div class="table-header">
        <div class="left">
          <span class="title">{{ activeTab === 'pending' ? '待处理纠纷列表' : '所有纠纷列表' }}</span>
        </div>
        <div class="right">
          <el-button type="primary" size="small" @click="fetchData">刷新</el-button>
        </div>
      </div>

      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="加载中..."
        border
        fit
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column label="订单编号" prop="orderNo" align="center" width="180">
          <template #default="scope">
            <router-link :to="'/admin/order/detail/' + scope.row.orderId" class="link-type">
              {{ scope.row.orderNo }}
            </router-link>
          </template>
        </el-table-column>
        <el-table-column label="客户名称" prop="customerName" align="center" width="120" />
        <el-table-column label="纠纷类型" prop="disputeType" align="center" width="120" />
        <el-table-column label="纠纷描述" prop="description" align="center" width="300">
          <template #default="scope">
            <el-tooltip :content="scope.row.description" placement="top" :disabled="scope.row.description.length < 40">
              <span>{{ scope.row.description.length > 40 ? scope.row.description.slice(0, 40) + '...' : scope.row.description }}</span>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status" align="center" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" align="center" width="160">
          <template #default="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200" fixed="right">
          <template #default="scope">
            <el-button type="primary" size="small" @click="handleDetail(scope.row)">详情</el-button>
            <el-button 
              type="success" 
              size="small" 
              @click="handleProcess(scope.row)"
              v-if="scope.row.status === 'pending' || scope.row.status === 'processing'"
            >
              处理
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination-container">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="listQuery.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="listQuery.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        />
      </div>
    </el-card>

    <!-- 处理纠纷对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="600px">
      <el-form :model="disputeForm" label-width="100px">
        <el-form-item label="纠纷类型">
          <span>{{ currentDispute.disputeType }}</span>
        </el-form-item>
        <el-form-item label="客户">
          <span>{{ currentDispute.customerName }}</span>
        </el-form-item>
        <el-form-item label="问题描述">
          <div class="dispute-description">{{ currentDispute.description }}</div>
        </el-form-item>
        <el-form-item label="处理状态">
          <el-select v-model="disputeForm.status" placeholder="请选择处理状态" style="width: 100%">
            <el-option label="处理中" value="processing" v-if="currentDispute.status === 'pending'" />
            <el-option label="已解决" value="resolved" v-if="currentDispute.status !== 'rejected'" />
            <el-option label="已拒绝" value="rejected" v-if="currentDispute.status !== 'resolved'" />
          </el-select>
        </el-form-item>
        <el-form-item label="解决方案">
          <el-input v-model="disputeForm.resolution" type="textarea" :rows="4" placeholder="请输入解决方案或拒绝理由" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitDisputeProcess">提交</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPendingDisputeList, getAllDisputeList, processDispute } from '@/api/orderManagement'
import { formatDate } from '@/utils/index'

export default {
  name: 'DisputeList',
  data() {
    return {
      activeTab: 'pending',
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        size: 10,
        status: '',
        keyword: ''
      },
      dialogVisible: false,
      currentDispute: {},
      disputeForm: {
        status: '',
        resolution: ''
      }
    }
  },
  computed: {
    dialogTitle() {
      return this.currentDispute.status === 'pending' ? '处理新纠纷' : '更新纠纷状态'
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      
      const request = this.activeTab === 'pending' 
        ? getPendingDisputeList({ page: this.listQuery.page - 1, size: this.listQuery.size })
        : getAllDisputeList({ 
            page: this.listQuery.page - 1, 
            size: this.listQuery.size,
            status: this.listQuery.status,
            keyword: this.listQuery.keyword
          })
      
      request.then(response => {
        this.list = response.data.content
        this.total = response.data.totalElements
        this.listLoading = false
      }).catch(() => {
        this.listLoading = false
      })
    },
    handleTabChange() {
      this.resetFilter()
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.fetchData()
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        size: 10,
        status: '',
        keyword: ''
      }
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchData()
    },
    getStatusType(status) {
      const statusMap = {
        'pending': 'danger',
        'processing': 'warning',
        'resolved': 'success',
        'rejected': 'info'
      }
      return statusMap[status] || 'info'
    },
    getStatusText(status) {
      const statusMap = {
        'pending': '待处理',
        'processing': '处理中',
        'resolved': '已解决',
        'rejected': '已拒绝'
      }
      return statusMap[status] || '未知状态'
    },
    handleDetail(row) {
      this.$router.push(`/admin/order/dispute/${row.id}`)
    },
    handleProcess(row) {
      this.currentDispute = row
      this.disputeForm = {
        status: row.status === 'pending' ? 'processing' : '',
        resolution: ''
      }
      this.dialogVisible = true
    },
    submitDisputeProcess() {
      if (!this.disputeForm.status) {
        this.$message.error('请选择处理状态')
        return
      }
      
      if (!this.disputeForm.resolution && (this.disputeForm.status === 'resolved' || this.disputeForm.status === 'rejected')) {
        this.$message.error('请输入解决方案或拒绝理由')
        return
      }
      
      const data = {
        status: this.disputeForm.status,
        resolution: this.disputeForm.resolution,
        operatorId: this.$store.getters.userId // 假设有用户ID存储在Vuex中
      }
      
      processDispute(this.currentDispute.id, data)
        .then(() => {
          this.$message.success('纠纷处理成功')
          this.dialogVisible = false
          this.fetchData()
        })
        .catch(err => {
          this.$message.error('纠纷处理失败: ' + err.message)
        })
    },
    formatDate
  }
}
</script>

<style scoped>
.filter-container {
  margin-bottom: 20px;
}
.form-inline {
  margin-top: 20px;
}
.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.title {
  font-size: 18px;
  font-weight: bold;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
.link-type {
  color: #409EFF;
  text-decoration: none;
}
.link-type:hover {
  text-decoration: underline;
}
.dispute-description {
  padding: 10px;
  background-color: #f5f7fa;
  border-radius: 4px;
  min-height: 60px;
}
</style> 